from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import render
from utils.access_restriction_new import access_rt
from user_system import models

class check_permissions(MiddlewareMixin):
        def process_request(self,request):
            #print("请求来了1")
            pass

        def process_view(self,request,view_func,view_func_args,view_func_kargs):
            #print("匹配路由映射关系成功后执行所有的process_view")
            permissions_status = access_rt(request, models)
            if not permissions_status:
                return render(request, "super_cmdb/permission_denied.html")
            else:
                pass

        def process_response(self,request,response):
            #print("响应请求",request.path)
            # permissions_status = access_rt(request,models)
            # if not permissions_status:
            #     return render(request,"super_cmdb/permission_denied.html")
            # else:
            #     return response #响应请求
            # 全局设置跨域问题
            # response['Access-Control-Allow-Origin'] = '*'
            return response
            #return HttpResponse("不允许你访问，走开！")   #限制访问，使用判断语句

        def process_exception(self,request,exception):  #如果view函数执行出错则会执行此函数，捕捉异常
            # if isinstance(exception,ValueError):
            #     return HttpResponse("出现异常：",exception)
            pass
